package HelperFinancial.User;


import java.util.List;
import HelperFinancial.DataBase.CategoriaAdapter;
import HelperFinancial.DataBase.ConfigurationAdapter;
import HelperFinancial.DataBase.DebitoAdapter;
import HelperFinancial.DataBase.DebitoHelper;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ListarDebitosActivity extends Activity {
	
	ListView list;
    private ListarDebitosActivity frame = this;
	Cursor model=null;
	DebitoListAdapter adapter=null;
	DebitoAdapter adapterGeral;
	DebitoHelper helper;
	CategoriaAdapter categoriaAdapter;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lista_debitos);
		
		adapterGeral = DebitoAdapter.getInstance(this);
		helper=new DebitoHelper(this);
		categoriaAdapter = CategoriaAdapter.getInstance(frame);
		
		ConfigurationAdapter configAdapter = ConfigurationAdapter.getInstance(frame);
		
		list=(ListView)findViewById(R.id.debits);
		if (configAdapter.getCategory().equalsIgnoreCase("Todos")) {
			model=helper.getDebitsByFilter(configAdapter.getDateInit());	
		} else {
			model=helper.getDebitsByFilter(configAdapter.getDateInit(), configAdapter.getCategory());
		}
		startManagingCursor(model);
		adapter=new DebitoListAdapter(model);
		list.setAdapter(adapter);
		list.setOnItemClickListener(onListClick);
	        
	}
	
	private AdapterView.OnItemClickListener onListClick = new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View row, int position,
				long id) {
			model.moveToPosition(position);
			Intent intent = new Intent(frame, GastosViewActivity.class);
			Integer id_Gasto = model.getInt(0);
			if (id_Gasto != null && id > 0) {
				intent.putExtra("id", model.getInt(0));
				finish();
				startActivity(intent);
			}

		}

	};

	
	@Override
	public void onDestroy() {
		super.onDestroy();
		helper.close();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_configuration, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.filter) {
			finish();
			startActivity(new Intent(this, DateUpdateActivity.class));
			return true;
		}

		if (item.getItemId() == R.id.redefine_default) {
			redefinirPadrao();
			finish();
			startActivity(new Intent(this, ExtractActivity.class));
			return true;
		}
		return (super.onOptionsItemSelected(item));
	}
	
	private void redefinirPadrao() {
		ConfigurationAdapter config = ConfigurationAdapter.getInstance(frame);
		config.populate();

	}

	private void selecionarCategoria() {
		List<String> categorias = categoriaAdapter.getAllCategorys();
		final String[] items = new String[categorias.size()];
		for (int i = 0; i < items.length; i++) {
			items[i] = categorias.get(i);
		}
		AlertDialog.Builder builder = new AlertDialog.Builder(frame);
		builder.setTitle("Selecione a Categoria");
		builder.setItems(items, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int item) {
				String categoria = items[item];
				ConfigurationAdapter config = ConfigurationAdapter.getInstance(frame);
	            String id = config.getId();
	            config.updateCategory(categoria, id);
			}
		});
		AlertDialog alert = builder.create();
		alert.show();
	}

	
	class DebitoListAdapter extends CursorAdapter {
		
		DebitoListAdapter(Cursor c) {
		super(ListarDebitosActivity.this, c);
		
		}
		
		@Override
		public void bindView(View row, Context ctxt, Cursor c) {
			DebitoHolder holder=(DebitoHolder)row.getTag();
			
			holder.populateFrom(c, helper);
		}

		@Override
		public View newView(Context ctxt, Cursor c, ViewGroup parent) {
			LayoutInflater inflater=getLayoutInflater();
			View row=inflater.inflate(R.layout.debito_row, parent, false);
			DebitoHolder holder=new DebitoHolder(row);
			row.setTag(holder);
			return(row);
		}
		
	}
	
	static class DebitoHolder {
		private TextView name=null;
		private TextView value=null;
		private TextView type=null;
		private ImageView icon=null;
		private View row=null;
		
		DebitoHolder(View row) {
			this.row=row;
			name=(TextView)row.findViewById(R.id.debit_name);
			value=(TextView)row.findViewById(R.id.debit_value);
			type=(TextView)row.findViewById(R.id.debit_type);
			icon=(ImageView)row.findViewById(R.id.icon);
		}
		
		void populateFrom(Cursor c, DebitoHelper helper) {
			name.setText(helper.getDebitName(c));
			value.setText(String.valueOf(helper.getDebitValue(c)));
			type.setText(String.valueOf(helper.getDebitType(c)));
			if (helper.getDebitType(c).equals("sit_down")) {
				icon.setImageResource(R.drawable.debitos);
			}
			else if (helper.getDebitType(c).equals("take_out")) {
				icon.setImageResource(R.drawable.debitos);
			}
			else {
				icon.setImageResource(R.drawable.debitos);
			}
		}
		
	}	
	
}